import Common from '../common'
var Js = Common('withdraws')
export default {
	name: 'withdraws',
	mixins: [Js],
	components: {},
	data() {
		return {
			table_arguments: {
				height: '70%',
				row_key: 'id',
				tree_props: {
					children: 'child',
					child: 'child',
					hasChildren: 'child'
				},
				default_expand_all: false,
				expand: false,
				selection: true,
				cell_style: {}
			},
			fields: [{
					key: 'id',
					label: 'ID',
					application: [],
					componentName: 'sls-input',
					listType: '',
					width: 50
				},
				{
					key: 'trade_info',
					label: '说明',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
				},
				{
					key: 'count',
					label: '提现金额',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					width: 120,
				},
				{
					key: "withdraw_status",
					label: '提现状态',
					application: ['编辑', '添加'],
					listType: 'normal',
					formatter: function(val, tests) {
						if (val.withdraw_status == -1) {
							return '拒绝';
						} else if (val.withdraw_status == 1) {
							return '成功提现';
						} else {
							return '申请中';
						}
					},
					componentName: 'sls-select',
					optionsName: 'withdrawOptions',
					filter_multiple: false,
					defaultProps: {
						label: 'text',
						value: 'value',
					},
					header_search: {
						componentName: 'sls-select',
						optionsName: 'withdrawOptions',
						style: 'width:160px;margin-right:2px;',
						placeholder: '请选择提现状态',
						changeFunc: function(val, self) {
							if (val || val === 0) {
								self.searchItem.withdraw_status = val;
							} else {
								delete self.searchItem.withdraw_status;
							};
						},
					},
					width: 100
				},
				{
					key: 'mainImg',
					label: '收款码',
					customSlot: 'mainImg',
					application: ['编辑', '添加'],
					componentName: 'upload',
					listType: 'normal',
					limit: 10,
					dialogStyle: 'width:90%',
					width: 130
				},
				{
					key: 'name',
					label: '用户',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					formatter: function(val, tests) {
						return val.user.nickname ? val.user.nickname : '';
					},
					header_search: {
						componentName: 'sls-input',
						style: 'width:160px;margin-right:2px;',
						placeholder: '请输入提现用户',
						changeFunc: function(e, self) {
							if (e) {
								self.User.searchItem.name = ['LIKE', ['%' + e + '%']]
							} else {
								delete self.User.searchItem.name
							};
							self.beforeSearch('User');
						},
					},
					width: 200
				},
				{
					key: 'login_name',
					label: '用户手机号',
					application: [],
					componentName: 'sls-input',
					listType: 'normal',
					formatter: function(val, tests) {
						return val.user.login_name ? val.user.login_name : '';
					},
					header_search: {
						componentName: 'sls-input',
						style: 'width:160px;margin-right:2px;',
						placeholder: '请输入手机号',
						changeFunc: function(e, self) {
							if (e) {
								self.User.searchItem.login_name = ['LIKE', ['%' + e + '%']]
							} else {
								delete self.User.searchItem.login_name
							};
							self.beforeSearch('User');
						},
					},
					width: 120
				},
				// {
				// 	key: 'reson',
				// 	label: '拒绝原因',
				// 	application: ['拒绝'],
				// 	componentName: 'sls-textarea',
				// 	listType: 'normal',
				// },
				{
					key: 'create_time',
					label: '申请时间',
					listType: 'normal',
					placeholder: '请选择申请时间',
					header_search: {
						componentName: 'sls-date-time-range',
						style: 'width:160px;margin-right:2px;',
						start_placeholder: '申请开始时间',
						end_placeholder: '申请结束时间',
						changeFunc: function(value, self) {
							if (!value) {
								delete self.searchItem.create_time;
							} else {
								self.searchItem.create_time = ['between', value = value.map(function(e) {
									return e / 1000;
								})]
							};

						},
					},
					width: 200,
				},
				{
					label: '操作',
					listType: 'deal',
					width: 140
				},
			],
			// 按钮配置
			btn_info: [
				// {
				// 	type: 'info',
				// 	icon: 'edit',
				// 	size: 'mini',
				// 	funcType: 'func',
				// 	position: 'list',
				// 	text: function(data) {
				// 		return '同意'
				// 	},
				// 	styleFunc: function(data) {
				// 		if (data.withdraw_status == 0) {
				// 			return 'background-color: #029A15;border-color:#029A15'
				// 		} else {
				// 			return 'display: none'
				// 		}
				// 	},
				// 	func: {
				// 		formData: function(self) {
				// 			return self.formData;
				// 		},
				// 		func: function(self) {
				// 			self.payToPerson(self.formData);
				// 		},
				// 	},
				// },
				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					funcType: 'submit',
					position: 'list',
					text: function(data) {
						return '同意'
					},
					styleFunc: function(data) {
						if (data.withdraw_status === 0) {
							return 'background-color: #029A15;border-color:#029A15'
						} else {
							return 'display: none'
						}
					},
					func: {
						apiName: function(self) {
							return "api_flowLogUpdate"
						},
						formData: function(self) {
							return self.formData
						},
						postData: function(self) {
							var postData = {
								searchItem: {
									id: self.formData.id,
								},
								data: {
									withdraw_status: 1,
								}
							};
							return postData;
						}
					},
				},
				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					funcType: 'submit',
					position: 'list',
					text: function(data) {
						return '拒绝'
					},
					styleFunc: function(data) {
						if (data.withdraw_status == 0) {
							return 'background-color: #df231c;border-color:#df231c'
						} else {
							return 'display: none'
						}
					},
					func: {
						apiName: function(self) {
							return "api_flowLogUpdate"
						},
						formData: function(self) {
							return self.formData
						},
						postData: function(self) {
							var postData = {
								searchItem: {
									id: self.formData.id,
								},
								data: {
									withdraw_status: -1,
								},
								saveAfter: [{
									tableName: 'FlowLog',
									FuncName: 'add',
									data: {
										type: self.formData.type,
										count: -parseFloat(self.formData.count),
										trade_info: '提现拒绝(退回)',
										account: 1,
										thirdapp_id: 2,
										user_no: self.formData.user_no,
									}
								}, 
								// {
								// 	tableName: 'FlowLog',
								// 	FuncName: 'add',
								// 	data: {
								// 		type: self.formData.type,
								// 		count: -parseFloat(self.formData.radio.count),
								// 		trade_info: '提现手续费(退回)',
								// 		account: 1,
								// 		thirdapp_id: 2,
								// 		user_no: self.formData.user_no,
								// 	}
								// },
								]
							};
							return postData;
						}
					},
				},
			],

			searchItem: {
				type: ['in',[2,3]],
				withdraw: 1
			},
			startSearchItem: {
				type: ['in',[2,3]],
				withdraw: 1
			},
			optionData: {
				typeOptions: [{
					text: '微信提现',
					value: 1
				}, {
					text: '银行卡提现',
					value: 2
				}],
				applyOptions: [{
						text: '拒绝',
						value: -1
					},
					{
						text: '待审核',
						value: 0
					},
					{
						text: '同意',
						value: 1
					},
				],
			},
			otherData: {},
			UserInfo: {
				tableName: 'UserInfo',
				searchItem: {},
				key: 'user_no',
				middleKey: 'user_no',
				condition: 'in',
			}
		}

	},

	mounted() {
		const self = this;
		 
		this.init()
	},
	computed: {
		token: function() {
			return this.$store.getters.getToken
		}
	},
	methods: {

		/**
		 * 初始化
		 */
		init() {
			this.initMainData();
		},

		//同意提现
		async payToPerson(item) {
			const self = this;
			const postData = {};
			postData.thirdapp_id = 2;
			postData.config_id = 1;
			postData.flowlog_id = item.id;
			postData.type = 1;
			postData.openid = item.openid;
			postData.user_no = item.user_no;
			postData.count = parseFloat(item.count);
			postData.desc = '提现';
			postData.token = self.$store.getters.getToken;
			this.$confirm('是否确定此操作?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(async () => {
				var res = await self.$$api_payToPerson({
					data: postData
				});
				if (res) {
					self.dialog.dialogFormVisible = false;
					if (res.result_code == 'FAIL') {
						self.$message({
							type: 'waring',
							message: res.messages
						});
					} else {
						self.$notify({
							title: '提现成功',
							type: 'success'
						});
						self.updateFlow(item);
					}
				};
			}).catch((e) => {
				console.log(e)
				self.$message({
					type: 'info',
					message: '操作失败'
				});
			});
		},

		//同意提现
		async transferAli(item) {
			const self = this;
			const postData = {};
			postData.thirdapp_id = 2;
			postData.flowlog_id = item.id;
			postData.config_id = 3;
			postData.price = -parseFloat(item.count);
			postData.identity_type = 'ALIPAY_LOGON_ID';
			postData.order_title = '提现';
			postData.identity = item.passage1;
			postData.name = item.name;
			postData.token = self.$store.getters.getToken;
			this.$confirm('是否确定此操作?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(async () => {
				var res = await self.$$api_transferAli({
					data: postData
				});
				if (res) {
					self.dialog.dialogFormVisible = false;
					if (res.status == 'success') {
						self.$notify({
							title: '提现成功',
							type: 'success'
						});
					} else {
						var data = JSON.parse(res.responseResult);
						self.$notify({
							title: '提现失败',
							content: '提现失败：' + data.alipay_fund_trans_uni_transfer_response
								.sub_code,
							type: 'fail'
						});
					}
				};
			}).catch((e) => {
				console.log(e)
				self.$message({
					type: 'info',
					message: '操作失败'
				});
			});
		},

		async updateFlow(item) {
			const self = this;
			const postData = {};
			postData.token = self.$store.getters.getToken;
			postData.searchItem = {
				id: item.id
			}
			postData.data = {
				withdraw_status: 1
			}
			var res = await self.$$api_flowLogUpdate({
				data: postData
			});
			self.initMainData();
		},

		/**
		 * 列表主函数
		 */
		async initMainData(isNew) {

			const self = this;
			const postData = {};
			if (isNew) {
				self.paginate.currentPage = 1;
			};
			postData.paginate = self.$$cloneForm(self.paginate);
			postData.token = self.$store.getters.getToken;
			if (self.searchItem) {
				postData.searchItem = self.$$cloneForm(self.searchItem)
			};
			if (JSON.stringify(self.getBefore) != "{}") {
				postData.getBefore = self.$$cloneForm(self.getBefore);
			};
			postData.getAfter = {
				user: {
					tableName: 'User',
					middleKey: 'user_no',
					key: 'user_no',
					condition: '=',
					searchItem: {
						status: 1
					},
					info: ['login_name', 'nickname'],
				},
				radio: {
					tableName: 'FlowLog',
					middleKey: 'id',
					key: 'relation_id',
					condition: '=',
					searchItem: {
						status: 1
					},
					info: ['count', 'trade_info', 'user_no'],
				},
			};

			var res = await self.$$api_flowLogGet({
				data: postData
			});
			self.mainData = res.info.data;
			self.paginate.count = res.info.total;

		}

	},


}
